package cn.sfa.service.smsmessage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.DateUtil;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.TableNameEnum;
import cn.sfa.entity.smsmessage.SaleSmsMessage;
import cn.sfa.service.meeting.MeetingService;

/**
 * 信息发送
 * 
 * @author hui
 * 
 */
public class SmsmessageSerive {
	Logger logger = LoggerFactory.getLogger(SmsmessageSerive.class);

	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	MajorKeyGenerate majorKeyGenerate;

	public Pagination SeachMessage(int currentPage,// 页码
			int numPerPage,// 页尺寸
			String url,// 分页url
			String recvmobileno, // 手机号码
			String recvcustomerid, //收件人
			String senddate		//发送时间
	) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		String sqlStr ="select * from table(pkg_sfa_smsmessage.Tosmsmessage(:recvmobileno,:recvcustomerid,:senddate))";
		paraMap.put("recvmobileno",recvmobileno);
		paraMap.put("recvcustomerid", recvcustomerid);
		paraMap.put("senddate", senddate);
		
		return new Pagination(sqlStr, paraMap, currentPage,
				numPerPage, url, jdbcTemplate);
	}

	public void insertsmsmessage(String[][] info, 
			String validdatetime,
			SaleSmsMessage model
			) throws Exception {
		int count = 0;
		String sql = "";
		//处理时间得到时间
		 
		 
		int time = Integer.parseInt(validdatetime);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		int rowLength = info.length;
		for (int i = 0; i < rowLength; i++) {
			for(int j =0;j<info[i].length;j++){
					model.setSmsmessageid(majorKeyGenerate
							.keyGenerate(TableNameEnum.SALEPRODUCT));// 主键ID
					sql = "insert into smsmessage(smsmessageid,createdate,recvcustomerid,recvmobileno,smscontext,validdatetime,sendstatus)"
							+ "values"
							+ "('"
							+ model.getSmsmessageid()
							+ "',sysdate,'"
							+ info[i][0]
							+ "','"
							+ info[i][1]
							+ "','"
							+ info[i][2] + "',to_date(to_char(sysdate, 'yyyy-mm-dd HH24:mi:ss'),'yyyy-mm-dd HH24:mi:ss')+("+time+"/24/60),'SMSSENDSTATUS_2')";	
			}
			jdbcTemplate.update(sql, paramMap);
		}
		paramMap.put("smsmessageid", CommonUtil.jugeAndFixNull(model
				.getSmsmessageid()));
		paramMap.put("recvcustomerid", CommonUtil.jugeAndFixNull(model
				.getRecvcustomerid()));
		paramMap.put("recvmobileno", CommonUtil.jugeAndFixNull(model
				.getRecvmobileno()));
		paramMap.put("rowLength", rowLength);
		paramMap.put("count", count);
	}

	public void addsmsmessage(String[] send, SaleSmsMessage model,
			String context,
			String validdatetime
			) throws Exception {
		String sql = "";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		int rowLength = send.length;
		 
        int count = Integer.parseInt(validdatetime); 
      
		for (int i = 0; i < rowLength; i++) {
			model.setSmsmessageid(majorKeyGenerate
					.keyGenerate(TableNameEnum.SALEPRODUCT));// 主键ID
			sql = "insert into smsmessage(smsmessageid,createdate,recvmobileno,smscontext,validdatetime,sendstatus)"
					+ "values ('"
					+ model.getSmsmessageid()
					+ "',sysdate,'"
					+ send[i]
					+ "','" + context + "',to_date(to_char(sysdate, 'yyyy-mm-dd HH24:mi:ss'),'yyyy-mm-dd HH24:mi:ss')+("+count+"/24/60),'SMSSENDSTATUS_2')";
			jdbcTemplate.update(sql, paramMap);
		}
		paramMap.put("smsmessageid", CommonUtil.jugeAndFixNull(model
				.getSmsmessageid()));
		paramMap.put("recvmobileno", CommonUtil.jugeAndFixNull(model
				.getRecvmobileno()));
		paramMap.put("context", CommonUtil
				.jugeAndFixNull(model.getSmscontext()));
		paramMap.put("rowLength", rowLength);
		
	}
}
